import pyautogui as pyg
import pygetwindow as gw
import keyboard
import time 

# 打包方法 pyinstaller.exe --onefile 自动锻造.py
pause=False 

DNF_WD = gw.getWindowsWithTitle("Dungeon & Fighter")[0]

#gw.activate()
#print(f"title:{DNF_WD}")
INIT_WIDTH=800
INIT_HEIGHT=600
def compute_position(x_offset,y_offset):
    y= DNF_WD.top+(INIT_HEIGHT/2+y_offset)*(DNF_WD.height/INIT_HEIGHT)
    x= DNF_WD.left+(INIT_WIDTH/2+x_offset)*(DNF_WD.width/INIT_WIDTH)
    return (x,y)
def get_position():
    try:
        # 获取当前鼠标的坐标
        x, y = pyg.position()
        print(f"Mouse position: X={x}, Y={y}")
        time.sleep(1)
    except KeyboardInterrupt:
        print("Program terminated.")

def click_dnf(x,y):
    pyg.moveTo(x,y,duration=0.3)
    pyg.mouseDown(button="left")
    pyg.mouseUp(button="left")

def pause():
    global pause 
    pause =  True
    print("暂停增福")
def start():
    global pause 
    pause =  False
    print("开始增福")
        
def duanzao():
    #锻造按钮位置
    (x,y)=compute_position(-92,51)
    click_dnf(x,y)
    #移动到装备第一格
    (x,y)=compute_position(85,-10)
    click_dnf(x,y)
    # 确认锻造  X=490, Y=375
    (x,y)=compute_position(90,75)
    click_dnf(x,y)
    time.sleep(7.5)
    #失败确认位置 410 350
    #(x,y)=compute_position(0,30)
    #click_dnf(x,y)
    # 失败确认位置2 406, Y=352
    (x,y)=compute_position(5,50)
    click_dnf(x,y)
    time.sleep(0.5)
keyboard.add_hotkey("ctrl+f11",start)
keyboard.add_hotkey("ctrl+f12",pause)
print("1,请按ctrl+f11开始 ，ctrl+f12停止锻造")
print("2.打开锻造界面")
print("6.按ctrl+f11开始锻造")
print("7.按ctrl+f12停止锻造")
while True:
    if pause:
        time.sleep(1)
    else:
        # 锻造开关
        duanzao()
        
